<?php

error_reporting(E_ALL);
ini_set("display_errors",1);
/* a note about the zip function:

I assume that this server is using PHP 5.2.0 or better. However, if its using an older version of PHP, all is not lost - just download and use the ext/zip extension from the PECL repository, at http://pecl.php.net/package/zip

*/
//some house cleaning stuff

// remove all previous created QR Codes
$dirToList = './qrOutput/';
deleteDirFiles($dirToList);


function deleteDirFiles ($dir) {

if ($handle = opendir($dir)) {
		while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            //echo "$file\n";
            unlink($dir.'/'.$file);
        }        
              }
    
    closedir($handle);
}
} // end of house cleaning

// Where the file is going to be placed 
$target_path = "uploads/";

/* Add the original filename to the target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

//build the HTML
echo '<html><head><title>QR Code Creation Utility</title></head><body>';
//try to move the file from TMP to the target Dir
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "<br>The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
    echo '<br><h4> beginning processing...</h4>';
//echo '<strong>Begin Testing Output:</strong><br>'; //debuging
$file = file($target_path); 
	$counter = 0;
    foreach ($file as $line) { // Process each line
    $exp = explode(";",$line); // Explode by commas
    // android and iphone barcode
    //$vcard_file = fopen($exp[0]." ".$exp[1].".vcf","w"); // Open VCard file for reading (Format: Firstname Lastname.vcf)
    $vcard_contents = 
"BEGIN:VCARD 
VERSION:2.1
N:$exp[1];$exp[0]
FN:$exp[0] $exp[1]
TITLE:$exp[2]
TEL:$exp[3]
EMAIL:$exp[4]
ADR:;;$exp[5];$exp[6];$exp[7];$exp[8];
URL:$exp[9]
REV:20110407T103000Z 
END:VCARD";
    //fwrite($vcard_file,$vcard_contents); // Write contents to VCard file
   // fclose($vcard_file); // Close VCard file
   $vcfUrlEncode = urlencode($vcard_contents);
   $url= 'https://chart.googleapis.com/chart?cht=qr&chs=200x200&chl=' .$vcfUrlEncode;
   $imageData = file_get_contents($url); 
   file_put_contents('qrOutput/'.$exp[1].'_'.$exp[0].'_vA.png',$imageData);
   //*************************
    // Blackberry and Windows Creation
    //*************************
    $vcard_contents ="";
    $vcfUrlEncode = "";
    $url = "";
    $imageData = "";
    
    $vcard_contents = "http://www.sparqcode.com/qrgen?qt=vcard&fName=$exp[0]&lName=$exp[1]&title=$exp[2]&tel=$exp[3]&email=$exp[4]@wslife.com&address=$exp[5]&city=$exp[6]&state=$exp[7]&zip=$exp[8]&url=$exp[9]";
     //$vcfUrlEncode = urlencode($vcard_contents);
     $vcard_contents = trim($vcard_contents);
     $url = preg_replace('/\s/','+',$vcard_contents);
   // echo($url);
   // echo'<br>';
   $imageData = file_get_contents($url); 
  file_put_contents('qrOutput/'.$exp[1].'_'.$exp[0].'_vB.gif',$imageData);
   $counter = $counter+1; 
   }
echo '<br><br><img src="images/MightyMouse.jpeg"><br><br>Woot! Job Done! <br>'.$counter.' records processed.<br><br>

<form enctype="" action="downloadQrCodes.php" method="POST">
Click here to download a ZIP archive of your QR Codes in PNG format.<br><br>
<input type="submit" value="Download ZIP" />
</form>


</body></html>';

} else{
    echo '<img src=\'images\Mr_Yuck.png\' width=\'100\' height=\'100\'> <br><p>There was an error uploading the file, please go back and try again!</p> <p> <a href=\'cvsUploadForQrCode.php\'>Return to CVS upload page</a>';
}

?>